package org.turman.buobaobook.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.turman.buobaobook.model.ResponseBean;
import org.turman.buobaobook.model.request.SendSimpleEmailBean;
import org.turman.buobaobook.service.EmailService;

@RestController
@RequestMapping("/mail")
public class MailController {
    @Autowired
    private EmailService mailService;

    @PostMapping("/send")
    public ResponseBean sendMail(@RequestBody SendSimpleEmailBean sendEmailBean){
        if (sendEmailBean==null) {
            return ResponseBean.fail("参数错误");
        }

        if (StringUtils.isEmpty(sendEmailBean.getTo())) {
            return ResponseBean.fail("请填写收件人");
        }
        if (StringUtils.isEmpty(sendEmailBean.getSubject())) {
            return ResponseBean.fail("请填写邮件主题");
        }

        if (mailService.send(sendEmailBean.getTo(),sendEmailBean.getSubject(),sendEmailBean.getContent())) {
            return ResponseBean.success();
        }else {
            return ResponseBean.fail("邮件发送失败");
        }
    }
}
